﻿using System.Data.SqlClient;
using System.ServiceModel;
using System.Text;
using System.Web.Configuration;
using ShellCommandService.Contracts.DataContracts;
using ShellCommandService.Contracts.ServiceContracts;

namespace ShellCommandService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class ShellCommandResultLogger: IShellCommandResultLogger
    {
        public void LogResult(ShellCommandResult commandResult)
        {
            DBHelper.ExecuteSqlCommand(BuildShellCommandResultLogSql(commandResult));
        }

        private string BuildShellCommandResultLogSql(ShellCommandResult result)
        {
            StringBuilder commandBuilder = new StringBuilder();
            commandBuilder.Append(
                "insert into [ShellCommands].[dbo].[ShellCommandRun]([CommandResult] ,[ResultType] ,[MachineName],[Command],[ExecutionTime])");
            commandBuilder.Append(" values(");
            commandBuilder.Append(" '" + result.Result + "','" + result.Type.ToString() + "','" + result.MachineName + "','" +
                                  result.Command + "','" + result.ExecutionTime + "'");
            commandBuilder.Append(")");
            return commandBuilder.ToString();
        }
    }
}
